package com.dyzx.hbb.modules.sys.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.sys.entity.SysBroker;
import com.dyzx.hbb.modules.sys.query.BrokerQuery;
import com.dyzx.hbb.modules.sys.service.SysBrokerService;
import com.dyzx.hbb.modules.sys.vo.BrokerUVO;
import com.dyzx.hbb.modules.sys.vo.BrokerVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/sys/broker")
@RestController
@Tag(name = "15.报关行管理", description = "报关行相关接口")
public class BrokerController extends BaseController<SysBrokerService, SysBroker, BrokerQuery, BrokerVO, BrokerUVO> {

    @Autowired
    private SysBrokerService service;



    @Override
    protected SysBrokerService getBaseService() {
        return service;
    }



    @Override
    protected MyLambdaQueryWrapper<SysBroker> queryWrapper(BrokerQuery query) {
        MyLambdaQueryWrapper<SysBroker> wrapper = new MyLambdaQueryWrapper<>();


            wrapper.select(SysBroker::getId, SysBroker::getName, SysBroker::getType,
                     SysBroker::getLeader,SysBroker::getLeaderPhone,
                    SysBroker::getStatus, SysBroker::getCreateDate, SysBroker::getUpdateDate);
            wrapper.organName(SysBroker::getOrganId).as("organ_name");
                   // .imageUrl(SysBroker::getLicense).as("licenseUrl");
        if (query != null) {
            wrapper.like(StringUtils.isNotBlank(query.getName()), SysBroker::getName, query.getName())
                    .like(StringUtils.isNotBlank(query.getLeader()), SysBroker::getLeader, query.getLeader())
                    .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysBroker::getCreateDate, DateFormat.DATE, query.getCreateDate())
                    .eq(query.getType() != null, SysBroker::getType, query.getType())

                    .like(StringUtils.isNotBlank(query.getLeaderPhone()), SysBroker::getLeaderPhone, query.getLeaderPhone());


        }
        return wrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<SysBroker> queryInfoWrapper() {


        MyLambdaQueryWrapper<SysBroker> wrapper = new MyLambdaQueryWrapper<>();

        wrapper.select(SysBroker::getId, SysBroker::getName, SysBroker::getName, SysBroker::getType,
                 SysBroker::getAddress, SysBroker::getLeader, SysBroker::getLeaderPhone,

                SysBroker::getStatus, SysBroker::getCreateDate, SysBroker::getUpdateDate,SysBroker::getFullName,SysBroker::getImage,SysBroker::getFile, SysBroker::getAddress);
        wrapper.imageUrls(SysBroker::getImage).as("image_url").imageUrls(SysBroker::getFile).as("file_url").organName(SysBroker::getOrganId).as("organ_name");

        return wrapper;
    }

    @Operation(summary = "获取报关行简易列表")
    @GetMapping("/mini_list")
    public Result<List<SysBroker>> miniList(
       @RequestParam(defaultValue = "0") int type

    ) {
        MyLambdaQueryWrapper<SysBroker> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysBroker::getId,  SysBroker::getName);


            wrapper.eq(type != 0, SysBroker::getType, type);



        return Result.ok(getBaseService().list(wrapper));
    }

}
